
import validator from "/utils/validators";
import { freeSourceType, unitType } from "/utils/dictionary";
const app = getApp();

Component({
  data: {
    permissions: [],
    show: false,
    index: null,
    selectableList: [],
    freeSourceType,
    unitType, // 增值服务单位字典
    form: {
      freeServiceType: "FREE",
      sourceType: "",
      serviceIndex: null,
      name: null,
      count: "",
      freeServiceId: null
    }
  },
  props: {
    selectedServices: [],
    services: [],
    onChange: () => { }
  },
  didMount() {
    this.setData({
      permissions: app.globalData.permissions
    });
  },
  didUpdate() { },
  didUnmount() { },
  methods: {
    modifyServiceForm(e) {
      let form = JSON.parse(JSON.stringify(this.props.selectedServices[e.target.targetDataset.index]));
      this.setData({
        index: e.target.targetDataset.index,
        show: true
      });
      this.filterServiceSelecteable(form.freeServiceType);
      form.serviceIndex = this.data.selectableList.findIndex(item => item.name === form.name);
      this.setData({
        form: form
      });
    },
    onRemoveService(e) {
      dd.confirm({
        title: '提示',
        content: '您确定要删除吗？',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        success: (result) => {
          if (result.confirm) {
            this.props.onChange({
              index: e.target.targetDataset.index,
              form: null
            });
          }
        },
      });
    },
    catchMethod() { },
    onFormSubmit(e) {
      Object.assign(this.data.form, e.detail.value);
      let validate = new validator();
      validate.add("checkEmpty", this.data.form.freeServiceType, "类型不能为空");
      validate.add("checkEmpty", this.data.form.sourceType, "赠送类型不能为空")
      validate.add("checkEmpty", this.data.form.name, "服务名称不能为空");
      validate.add("checkIntNotZero", this.data.form.count, "数量不能为空");
      if (validate.check()) {
        this.data.form.totalPrice = this.data.form.count * this.data.selectableList[this.data.form.serviceIndex].price;
        this.props.onChange({
          index: this.data.index,
          form: this.data.form
        });
        this.setData({
          show: false
        })
      }
    },
    showModal() {
      this.setData({
        show: true,
        index: null,
        form: {
          freeServiceType: "FREE",
          sourceType: "",
          serviceIndex: null,
          name: null,
          count: "",
          freeServiceId: null
        }
      })
      this.filterServiceSelecteable('FREE')
    },
    hideModal() {
      this.setData({
        show: false,
        index: null,
        form: {
          serviceIndex: null,
          name: null,
          count: "",
          freeServiceId: null
        }
      })
    },
    filterServiceSelecteable(freeServiceType) {
      let selectableList = this.props.services.filter(element => {
        return !this.props.selectedServices.find((item, index) => {
          return this.data.index === null ? (item.name === element.name && item.freeServiceType === freeServiceType) : (item.name === element.name && item.freeServiceType === freeServiceType && this.data.index != index)
        })
      })
      this.setData({
        selectableList: selectableList
      });
    },
    onSorceTypeChange(e) {
      this.setData({
        "form.sourceType": e.detail.value
      })
    },
    onServiceChange(e) {
      this.setData({
        "form.serviceIndex": e.detail.value,
        "form.freeServiceId": this.data.selectableList[e.detail.value].id,
        "form.name": this.data.selectableList[e.detail.value].name,
        "form.unitPrice": this.data.selectableList[e.detail.value].price,
        "form.unit": this.data.selectableList[e.detail.value].unit,
        "form.count": this.data.selectableList[e.detail.value].ext1 ? this.data.selectableList[e.detail.value].ext1 : ""
      });
    },
    onCountInput(e) {
      this.setData({
        "form.count": e.detail.value
      });
    },
    onReduce() {
      if (this.data.selectableList[this.data.form.serviceIndex].ext1 != this.data.form.count) {
        this.data.form.count = +this.data.form.count - parseInt(this.data.selectableList[this.data.form.serviceIndex].ext1);
        this.setData({
          "form.count": this.data.form.count
        });
      }
    },
    onIncrease() {
      this.data.form.count = +this.data.form.count + parseInt(this.data.selectableList[this.data.form.serviceIndex].ext1);
      this.setData({
        "form.count": this.data.form.count
      });
    }
  },
});

